<template>
  <div class="app-container">
    <el-form v-show="true" ref="queryForm" :inline="true" :model="queryParams" label-width="68px">
      <el-form-item label="文件名称" prop="fileName">
        <el-input
          v-model="queryParams.fileName"
          clearable
          placeholder="请输入文件名称"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="attachList" @selection-change="handleSelectionChange">
      <el-table-column align="center" label="文件名称" prop="fileName" width="300"/>
      <el-table-column align="center" label="文件链接" prop="fileUrl" width="450"/>
      <el-table-column align="center" label="文件类型" prop="fileExt"/>
      <el-table-column align="center" label="文件大小" prop="fileSize"/>
      <el-table-column align="center" label="上传者" prop="createBy"/>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['system:attach:query']"
            icon="el-icon-document"
            size="mini"
            type="text"
            @click="handleView(scope.row)"
          >查看
          </el-button>
          <el-button
            v-hasPermi="['system:attach:download']"
            icon="el-icon-download"
            size="mini"
            type="text"
            @click="handleDownload(scope.row)"
          >下载
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />

    <!-- 添加或修改附件 对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="文件名称" prop="fileName">
          <el-input v-model="form.fileName" placeholder="请输入文件名称"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addAttach, listAttach, updateAttach } from '@/api/system/attach'
import { downloadFile, getPreviewUrl } from '@/api/oss/minio'

export default {
  name: 'Attach',
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 附件 表格数据
      attachList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        attachId: undefined,
        relateId: undefined,
        module: undefined,
        fileName: undefined,
        fileUrl: undefined,
        fileExt: undefined,
        fileSize: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询附件 列表 */
    getList() {
      this.loading = true
      listAttach(this.queryParams).then(response => {
        this.attachList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        attachId: undefined,
        relateId: undefined,
        module: undefined,
        fileName: undefined,
        fileUrl: undefined,
        fileExt: undefined,
        fileSize: undefined,
        delFlag: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.attachId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 查看按钮操作 */
    handleView(row) {
      getPreviewUrl(row.fileUrl).then(response => {
        window.open(response.data)
      })
    },
    handleDownload(row) {
      downloadFile(row.fileUrl).then(res => {
        const blob = new Blob([res])
        const fileName = row.fileName
        const link = document.createElement('a')
        link.download = fileName
        link.style.display = 'none'
        link.href = URL.createObjectURL(blob)
        document.body.appendChild(link)
        link.click()
        URL.revokeObjectURL(link.href)
        document.body.removeChild(link)
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.attachId != null) {
            updateAttach(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addAttach(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    }
  }
}
</script>
